<script setup lang="ts">
import {reactive, ref} from 'vue'
import {User, Lock} from "@element-plus/icons-vue";
import {ElNotification, type FormInstance, type FormRules} from "element-plus";
import Fitness from "@/components/icons/Fitness.vue";
import type {AdminLoginForm} from "@/type/admin.ts";
import {useAdminStore} from "@/stores/admin.ts";
import {useRouter} from "vue-router";

const ruleFormRef = ref<FormInstance>()

const checkInput = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('不可为空,请输入...'))
  } else {
    callback()
  }
}

const loginForm = reactive<AdminLoginForm>({
  username: '',
  password: ''
})

const rules = reactive<FormRules<typeof loginForm>>({
  username: [{validator: checkInput, trigger: 'blur'}],
  password: [{validator: checkInput, trigger: 'blur'}],
})

const adminStore = useAdminStore()

const router = useRouter()

const login = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      try {
        // 发送请求
        const result = await adminStore.adminLogin(loginForm)
        await router.push('/admin')
        ElNotification.success('登录成功')
      } catch (e) {
        ElNotification.error((e as Error).message)
      }
    } else {
      ElNotification.error('请检查输入')
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-main">
      <div class="header">
        <Fitness style="--icon-size: 1.5em; margin: 0"/>
        健身中心-管理端
      </div>
      <div class="login-form">
        <el-form ref="ruleFormRef" :model="loginForm" :rules="rules" :inline-message="true" label-width="auto"
                 style="max-width: 600px" status-icon>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" :prefix-icon="User"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password
                      :prefix-icon="Lock"/>
          </el-form-item>
          <el-form-item>
            <div class="login-button-container">
              <el-button class="login-button" @click="login(ruleFormRef)" color="#93ccc9">
                登录
              </el-button>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="to-register">
              有疑问？
              <RouterLink to="/about">联系我们</RouterLink>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="to-member">
              <RouterLink to="/memberLogin">我是用户</RouterLink>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  /*height: 100vh;*/
  height: 100%;
  background-image: linear-gradient(to right, #5ee7df, #b490ca);

  .login-main {
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    width: 400px;
    height: 560px;
    text-align: center;

    .header {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 38px;
      font-weight: bold;
      //text-align: center;
      line-height: 200px;
    }

    .login-form {
      padding: 0 20px;

      .login-button-container {
        display: flex;
        justify-content: center;
        width: 100%;
        padding-top: 15px;

        .login-button {
          width: 100%;
          border-radius: 5px;
        }
      }

      .to-register {
        display: flex;
        justify-content: center;
        align-items: center;
        padding-top: 20px;
        width: 100%;
      }
      .to-member {
        display: flex;
        justify-content: center;
        align-items: center;
        padding-top: 20px;
        width: 100%;
      }
    }
  }
}
</style>